#!/bin/bash
function submit {
if [ -a md/md.nohup ]; then
nstep=`tail -1 md/md.nohup | awk '{print $2}' | sed s/,//`
if [ "$nstep" = "" ]; then
 nstep=0
fi
thanx=`tail -10 md/md.nohup | egrep gcq | awk '{print $2}'`

iend=5000000
# 1 ns steps
#istep=500000
# 1.25 ns steps
istep=625000
# 2.5 ns steps
#istep=1250000
ns=1

echo "Run $j, step $nstep, `calc $nstep*0.002/1000` ns"
for ((i=$istep;i<=$iend;i+=$istep))
do
  if [ $nstep -gt $i ] || [ "$thanx" = "Thanx" ]; then
    if [ -a kb/md$ns.rdf ]; then
       filesize=`ls -l kb/md$ns.rdf | awk '{print $5}'`
       if [ "$filesize" = "0" ]; then
         echo "Job $j-kb$ns is running"
       else
         echo "Job $j-kb$ns is finished"
       fi
    else
       #Job is in the queue or has not been submitted
       jobid=`/opt/torque/bin/qstat -an | egrep $j-kb$ns | awk -F "." '{print $1}'`
       if [ -n "$jobid" ]; then
         #Job is in the queue
         echo "Job $j-kb$ns is in the queue"
       else
         #Job will be submitted
         /opt/torque/bin/qsub kb$ns.sh
       fi
    fi
  fi
  ns=`calc $ns+1`

done
fi
}

for j in md108
do
cd /home/bentenin/gromacs/mshmoh/skb/$j
submit $j
done

